package com.web.validator;

import com.core.dto.FileUploadDTO;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/4/18
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
@Component
public class FileUploadValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return FileUploadDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        FileUploadDTO fileUploadDTO = (FileUploadDTO) target;
        MultipartFile file = fileUploadDTO.getFile();
        if (file.getSize() > 1024 * 1024 * 10) {
            errors.rejectValue("file", null, "上传文件大小超出限制!");
        }
    }
}
